package com.example.mailsender.Service.imply;

import com.example.mailsender.Service.MailService;
import com.example.mailsender.dao.MailDao;
import com.sun.xml.internal.org.jvnet.mimepull.MIMEMessage;
import lombok.val;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import org.springframework.web.multipart.MultipartFile;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.IOException;
import java.io.InputStream;

@Service("MailService")
public class MailServiceImply implements MailService {
    @Autowired
    JavaMailSenderImpl javaMailSender;
    @Autowired
    TemplateEngine templateEngine;
    @Override
    public String sendMail(MailDao mailDao) {
        SimpleMailMessage msg = new SimpleMailMessage();
        msg.setFrom(mailDao.getMailNumberDeliver());
        msg.setTo(mailDao.getMailNumberReceiver());
        //邮件主题
        msg.setSubject(mailDao.getMailSubject());
        msg.setText(mailDao.getMailContent());
        System.out.println(mailDao);
        try {
            javaMailSender.send(msg);
        }catch (Exception e){
            System.out.println(e.getMessage());
            System.out.println("no");
            return "no";
        }

        return "ok";
    }

    @Override
    public String sendMailHtml(MailDao mailDao) throws MessagingException {
        MimeMessage mimeMessage = this.javaMailSender.createMimeMessage();
        MimeMessageHelper msg = new MimeMessageHelper(mimeMessage);
        msg.setFrom(mailDao.getMailNumberDeliver());
        msg.setTo(mailDao.getMailNumberReceiver());
        msg.setSubject(mailDao.getMailSubject());
        Context context = new Context();
        context.setVariable("userName",mailDao.getMailContent());
        context.setVariable("conpany",mailDao.getMailName());
        String process = templateEngine.process("GoFish.html", context);
        msg.setText(process,true);

        try {

            javaMailSender.send(mimeMessage);
        }catch (Exception e){
            System.out.println(e.getMessage());
            System.out.println("no");
            return "no";
        }

        return "ok";
    }

    @Override
    public String sendMailHtmlwithOther(MailDao mailDao, InputStreamResource file,String fileName) throws MessagingException, IOException {
        MimeMessage mimeMessage = this.javaMailSender.createMimeMessage();
        MimeMessageHelper msg = new MimeMessageHelper(mimeMessage,true);
        msg.setFrom(mailDao.getMailNumberDeliver());
        msg.setTo(mailDao.getMailNumberReceiver());
        msg.setSubject(mailDao.getMailSubject());
        Context context = new Context();
        context.setVariable("userName",mailDao.getMailContent());
        context.setVariable("conpany",mailDao.getMailName());
        String process = templateEngine.process("GoFish1.html", context);
        msg.setText(process,true);
        msg.addAttachment(fileName,new ByteArrayResource(IOUtils.toByteArray(file.getInputStream())));
        try {

            javaMailSender.send(mimeMessage);
        }catch (Exception e){
            System.out.println(e.getMessage());
            System.out.println("no");
            return "no";
        }

        return "ok";
    }
}
